<?php
namespace app\crm\model;
use think\Model;
use think\model\concern\SoftDelete;

class CrmContract extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    protected $autoWriteTimestamp = true;
    protected $globalScope = ['shop_id'];
    
    public function scopeShop_id($query)
    {
        $query->where('shop_id', ShopId());
    }
    //保存开始时间
    public function setStartTimeAttr($value)
    {
        return strtotime($value);
    }
    //读取开始时间
    public function getStartTimeAttr($value)
    {
        return date('Y-m-d',$value);
    }
    //保存结束时间
    public function setEndTimeAttr($value)
    {
        return strtotime($value);
    }
    //读取结束时间
    public function getEndTimeAttr($value)
    {
        return date('Y-m-d',$value);
    }
    //读取相册
    public function getPhotoAttr($value)
    {
        if(!$value){
            $list = array();
        }else{
            $list = explode(',', $value);
        }
        return $list;
    }

    public function setPhotoAttr($value)
    {
        if(!$value){
            $list = array();
        }else{
            $list = implode(',',array_filter($value));
        }
        return $list;
    }
    
    //关联用户
    public function user()
    {
        return $this->belongsTo(\app\user\model\User::class, 'user_id', 'id')->bind(['user_name'=>'name']);
    }
    //关联分类
    public function type()
    {
        return $this->belongsTo(\app\user\model\DictData::class, 'type_id', 'id')->bind(['type_name'=>'name']);
    }
    
}